package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidServer;
import com.apache.bid.manager.BidServerManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidServerManagerImpl implements BidServerManager {

	protected IDao bidServerDao;
	protected final String entityName = "com.apache.bid.entity.BidServer";

	public void setBidServerDao(IDao bidServerDao) {
		this.bidServerDao = bidServerDao;
	}

	public String saveInfo(ParamsVo<BidServer> vo) throws BusinessException {
		BidServer bidServer = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidServer_"+infoId;
		bidServer.setServerId(infoId);
		bidServer.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		MethodParam param = new MethodParam("BidServer", cacheKey, "", entityName);
		param.setVaule(bidServer);
		if (bidServerDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidServer> vo) throws BusinessException {
		BidServer bidServer = vo.getObj();
		if (Validator.isNotNull(bidServer.getServerId())) {
			String cacheKey = "";
			//String cacheKey="bidServer_"+bidServer.getServerId();
			MethodParam param = new MethodParam("BidServer", cacheKey, "", entityName);
			param.setVaule(bidServer);
			return bidServerDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidServer> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidServer_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidServer info = (BidServer) bidServerDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidServer");
			return bidServerDao.edit(param);
		} else {
			param.setParams("serverId", infoId);
			param.setDelete(true);
			return bidServerDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidServer> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidServer_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidServerDao.selectById(param);
	}

	public Object execute(ParamsVo<BidServer> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidServer> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = bidServerDao.pageSelect(param);
		return page;
	}

	public List<BidServer> getList(ParamsVo<BidServer> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return bidServerDao.select(param);
	}

	public long countInfo(ParamsVo<BidServer> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidServerDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidServer> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidServerDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidServer bidServer = vo.getObj();
		param.setResultCache(false);
		if (Validator.isNotNull(bidServer.getServerCname())) {
			sb.append(" and serverCname = :serverCname");
			param.setParams("serverCname", bidServer.getServerCname());
		}
		if (Validator.isNotNull(bidServer.getServerIp())) {
			sb.append(" and serverIp = :serverIp");
			param.setParams("serverIp", bidServer.getServerIp());
		}
		if (Validator.isNotNull(bidServer.getServerPort())) {
			sb.append(" and serverPort = :serverPort");
			param.setParams("serverPort", bidServer.getServerPort());
		}
		if (Validator.isNotNull(bidServer.getServerStatus())) {
			sb.append(" and serverStatus = :serverStatus");
			param.setParams("serverStatus", bidServer.getServerStatus());
		}
		if (Validator.isNotNull(bidServer.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidServer.getCreateTime());
		}
		if (Validator.isNotNull(bidServer.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidServer.getSpare1());
		}
		if (Validator.isNotNull(bidServer.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidServer.getSpare2());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
